/**
 * 
 */
package vn.smartware.co.framework.db.factory;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import vn.smartware.co.framework.bean.CommonDataDTO;

/**
 * @author Thuc
 *
 */
public class BaseDAO {

	/**
	 * Merge data from Resultset into List
	 * @param manager
	 * @return
	 * @throws SQLException
	 */
	public List<CommonDataDTO> getDataList(SQLManager manager) throws SQLException {
		List<CommonDataDTO> result = new ArrayList<CommonDataDTO>();
		ResultSet rs = manager.executeQuery();
		while (rs.next()){
			Map<String, Object> rowData = new HashMap<String,Object>();
			ResultSetMetaData metaData = rs.getMetaData();
			for (int i = 0; i < metaData.getColumnCount(); i++) {
				String columnName = metaData.getColumnName(i+1);
				Object value = rs.getObject(columnName);
				rowData.put(columnName, value);
			}
			CommonDataDTO data = new CommonDataDTO();
			data.setData(rowData);
			result.add(data);
		}
		return result;
	}
	
	/**
	 * Merge data from Resultset into Object
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	public CommonDataDTO getData(ResultSet rs) throws SQLException {
		CommonDataDTO data = new CommonDataDTO();
		while (rs.next()){
			Map<String, Object> rowData = new HashMap<String,Object>();
			ResultSetMetaData metaData = rs.getMetaData();
			for (int i = 0; i < metaData.getColumnCount(); i++) {
				String columnName = metaData.getColumnName(i+1);
				Object value = rs.getObject(columnName);
				rowData.put(columnName, value);
			}
			data.setData(rowData);
		}
		return data;
	}
}
